package com.sh.service.impl;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.sh.common.enums.OssTypeEnum;
import com.sh.common.pojo.bo.PutObjectRes;
import com.sh.config.OssClientConfig;
import com.sh.service.IOSSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

@Slf4j
public class AliOSSService implements IOSSService {

    private OssClientConfig ossClientConfig;
    private OSS ossClient;

    public AliOSSService(OssClientConfig ossClientConfig) {
        this.ossClientConfig = ossClientConfig;
        if (OssTypeEnum.ALI_OSS.getCode().equals(ossClientConfig.getResourceType())) {
            ossClient = new OSSClientBuilder().build(ossClientConfig.getEndpoint(),
                    ossClientConfig.getAccessKey(),
                    ossClientConfig.getSecretKey());
        }
    }

    @Override
    public InputStream getObject(String bucketName, String prefix) {
        return ossClient.getObject(bucketName, prefix).getObjectContent();
    }

    @Override
    public String signUrl(String bucketName, String objectName, Integer expires) {
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
        request.setExpiration(DateUtil.offsetDay(new Date(), expires));
        URL url = ossClient.generatePresignedUrl(request);
        return url.toString();
    }

    @Override
    public String signUrl(String objectName) {
        return signUrl(ossClientConfig.getBucket(), objectName, ossClientConfig.getSignExpires());
    }

    @Override
    public String signUrl(String objectName, Integer expires) {
        if (expires == null || expires < 1 || expires > 7) {
            expires = ossClientConfig.getSignExpires();
        }
        return signUrl(ossClientConfig.getBucket(), objectName, expires);
    }

    @Override
    public PutObjectRes putObject(String bucketName, String objectName, String content) {
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
            PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
            return PutObjectRes.builder()
                    .success(true)
                    .etag(putObjectResult.getETag())
                    .versionId(putObjectResult.getVersionId())
                    .build();
        } catch (Exception e) {
            log.error("put object error", e);
            return PutObjectRes.builder().success(false).build();
        }
    }

    @Override
    public PutObjectRes putObject(String objectName, MultipartFile file) {
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossClientConfig.getBucket(), objectName, file.getInputStream());
            PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
            return PutObjectRes.builder()
                    .success(true)
                    .etag(putObjectResult.getETag())
                    .versionId(putObjectResult.getVersionId())
                    .build();
        } catch (Exception e) {
            log.error("put object error", e);
            return PutObjectRes.builder().success(false).build();
        }
    }

    @Override
    public Boolean copyObject(String srcObject, String destObject) {
        try {
            CopyObjectRequest copyObjectRequest = new CopyObjectRequest(ossClientConfig.getBucket(), srcObject, ossClientConfig.getBucket(), destObject);
            ossClient.copyObject(copyObjectRequest);
            return true;
        } catch (Exception e) {
            log.error("复制文件失败", e);
            return false;
        }
    }

    @Override
    public PutObjectRes putObject(String objectName, String content) {
        return putObject(ossClientConfig.getBucket(), objectName, content);
    }


}
